{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# OpenCV\n",
    "\n",
    "OpenCV is a popular framework for image and video processing. On this tutorial,\n",
    "we show how OpenPifPaf can integrate with a workflow from OpenCV. OpenPifPaf\n",
    "also comes with a video tool for processing videos from files or usb cameras\n",
    "that is based on OpenCV, {ref}`openpifpaf.video <cli-help-video>`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "import openpifpaf\n",
    "\n",
    "%matplotlib inline\n",
    "openpifpaf.show.Canvas.show = True\n",
    "openpifpaf.show.Canvas.image_min_dpi = 200"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Source\n",
    "\n",
    "The `cv2.VideoCapture` class supports an enourmous amount of sources \n",
    "(files, cameras, rtmp, etc, ...) and abstracts the details away. Here, we will\n",
    "just pass in a single image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "capture = cv2.VideoCapture('coco/000000081988.jpg')\n",
    "_, image = capture.read()\n",
    "image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with openpifpaf.show.Canvas.image(image) as ax:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prediction\n",
    "\n",
    "Now that we have the image, we can use the `openpifpaf.Predictor` and then\n",
    "visualize its predicted annotations with an `AnnotationPainter`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor = openpifpaf.Predictor(checkpoint='shufflenetv2k16')\n",
    "predictions, gt_anns, meta = predictor.numpy_image(image)\n",
    "\n",
    "annotation_painter = openpifpaf.show.AnnotationPainter()\n",
    "with openpifpaf.show.Canvas.image(image) as ax:\n",
    "    annotation_painter.annotations(ax, predictions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example is intentionally left to be quite basic. If you are interested\n",
    "to accelerate this process with a GPU or you have many images that should be\n",
    "pre-loaded in parallel, please have a look at the {doc}`Prediction API <predict_api>` \n",
    "or use the {ref}`openpifpaf.video <cli-help-video>` command line tool."
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ea6946363a43e80d241452ab397f4c58bdd3d2517da174158e9c46ce6717422a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.4 64-bit ('venv3': venv)",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
